package zhao.ben.quan.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zhao.ben.quan.constant.MessageConstant;
import zhao.ben.quan.entity.PageResult;
import zhao.ben.quan.entity.QueryPageBean;
import zhao.ben.quan.entity.Result;
import zhao.ben.quan.pojo.CheckItem;
import zhao.ben.quan.service.CheckItmeService;

import java.util.List;


/**
 * 检查项管理
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference//查找服务
    private CheckItmeService checkItmeService;

    //新增方法
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        try{
            checkItmeService.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }

    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult add(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = checkItmeService.pageQuery(queryPageBean);

        return pageResult;

    }

    //删除预约项

    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//权限校验
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            checkItmeService.delete(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }


    //编辑预约项
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        try{
            checkItmeService.edit(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }


    //回显编辑预约项
    @RequestMapping("/findByid")
    public Result findByid(Integer id){
        try{
            CheckItem checkItem = checkItmeService.findByid(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<CheckItem> checkItem = checkItmeService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}
